package ink.yuanchu.picachoframework.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * 字段填充处理
 * @author ziyuan
 * @Date: 2021/03/11/15:47
 * @Description:
 */
@Component
public class MybatisPulseFillHandler implements MetaObjectHandler {

    /**
     * 日志
     */
    private static final Logger logger = LoggerFactory.getLogger(MybatisPulseFillHandler .class);

    /**
     * 插入时填充
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        logger.info("字段插入填充时间");
        //按名称设置字段值
        this.setFieldValByName("createTime",new Date(),metaObject);
        this.setFieldValByName("updateTime",new Date(),metaObject);
        this.setFieldValByName("version",1,metaObject);
    }

    /**
     * 更新时填充
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        logger.info("字段更新填充时间");
        this.setFieldValByName("updateTime",new Date(),metaObject);
    }

}
